﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;

namespace TieuLuanWindowsNC
{
    public partial class frmMain : Form
    {
        PhongBLL phongBLL = new PhongBLL();
        KhachHangBLL khBLL = new KhachHangBLL();
        HoaDonBLL hdBLL = new HoaDonBLL();
        ListViewItem item;
        DataGridViewRow dgvRow;

        public frmMain()
        {
            InitializeComponent();
        }

        private void themToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmThemKH frmThemKH = new frmThemKH();
            frmThemKH.MdiParent = this;
            frmThemKH.Show();
        }

        public void loadPhong()
        {
            lvPhong.Items.Clear();
            foreach (Phong p in phongBLL.getAllPhong())
            {
                var itemAdd = new ListViewItem();

                lvPhong.Items.Add(itemAdd.Text = p.MaPhong, itemAdd.ImageIndex = 0);
            }

            if (item != null)
            {
                //lvPhong.Items[item.Index].Selected = true;
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            loadPhong();
        }

        public void loadKHTrongPhong()
        {
            dgvKhachHang.DataSource = khBLL.findCustomersByRoom(item.SubItems[0].Text);
            dgvKhachHang.Columns[6].Visible = false;
            dgvKhachHang.Columns[7].Visible = false;
            
            //if (dgvRow != null)
            //{
            //    dgvKhachHang.Rows[item.Index].Selected = true;
            //}
        }

        private void lvPhong_Click(object sender, EventArgs e)
        {
            item = lvPhong.SelectedItems[0];
            
            loadKHTrongPhong();

            //if (dgvKhachHang.RowCount > 0)
            //{
            //    dgvRow = dgvKhachHang.SelectedRows[0];
            //}
        }

        private void thêmPhòngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmThemPhong frmthemPhong = new frmThemPhong();
            frmthemPhong.ShowDialog();
            loadPhong();
        }

        private void xóaPhòngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem item = lvPhong.SelectedItems[0];
            if (phongBLL.xoaPhong(item.Text) != true)
            {
                MessageBox.Show("Lỗi Không xóa được Phòng!");
            }
            loadPhong();
        }

        private void sửaThôngTinPhòngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem item = lvPhong.SelectedItems[0];

            frmSuaPhong frmSuaP = new frmSuaPhong();
            frmSuaP.ShowDialog();

            if (phongBLL.suaPhong(item.Text, frmSuaPhong.tinhTrang, frmSuaPhong.ghiChu) != true)
            {
                MessageBox.Show("Lỗi Không Sửa được Phòng!");
            }
            loadPhong();
        }

        private void btnThemKH_Click(object sender, EventArgs e)
        {
            ListViewItem item = lvPhong.SelectedItems[0];

            frmThemKH frmthemKH = new frmThemKH();
            frmThemKH.maPhong = layMaPhongDangChon();
            frmthemKH.ShowDialog();

            loadPhong();
        }

        public string layMaPhongDangChon()
        {
            ListViewItem item = lvPhong.SelectedItems[0];
            return item.Text;
        }

        private void btnXoaKH_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dgvKhachHang.SelectedRows[0];
            string maKH = row.Cells[0].Value.ToString();
            if (khBLL.xoaKH(maKH) != true)
                MessageBox.Show("Loi khi xoa Khach Hang");
            loadPhong();
        }

        private void btnSuaKH_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dgvKhachHang.SelectedRows[0];
            string maKH = row.Cells[0].Value.ToString();
            string tenKH = row.Cells[1].Value.ToString();
            string diaChi = row.Cells[2].Value.ToString();
            bool gioiTinh = bool.Parse(row.Cells[3].Value.ToString());
            string sdt = row.Cells[4].Value.ToString();
            string soCMND = row.Cells[5].Value.ToString();
            string maPhong = row.Cells[6].Value.ToString();
            string maLK = row.Cells[7].Value.ToString();

            KhachHang kh = new KhachHang(){
                MaKH = maKH,
                TenKH = tenKH,
                DiaChi = diaChi,
                GioiTinh = gioiTinh,
                SDT = sdt,
                SoCMND = soCMND,
                MaPhong = maPhong,
                MaLK = maLK
            };

            frmSuaKH.kh = kh;
            frmSuaKH frmsuaKH = new frmSuaKH();
            frmsuaKH.ShowDialog();

            loadKHTrongPhong();
        }

        private void dgvKhachHang_SelectionChanged(object sender, EventArgs e)
        {
            //dgvRow = dgvKhachHang.SelectedRows[0];
        }
    }
}
